package pomodorotasks

import ca.odell.glazedlists.swing.EventTableModel
import ca.odell.glazedlists.SortedList
import ca.odell.glazedlists.BasicEventList
import ca.odell.glazedlists.swing.EventTableModel
import ca.odell.glazedlists.gui.TableFormat

import javax.swing.*

//def createTableModel() {  
//    def columnNames = ["Title", "Updated", "Status"]
//    return new EventTableModel(model.tasksList, [  
//       getColumnCount: {columnNames.size()},  
//       getColumnName: {index -> columnNames[index]},  
//       getColumnValue: {object, index ->  
//          object."${columnNames[index].toLowerCase()}"  
//       }] as TableFormat)  
// }

def createStatusColumn() {
    return [name: "Status", 
        'class': Boolean, 
        read: { target, columns, index -> target.status == 'completed'}, 
        write: { target, columns, index, value -> target.status = value ? 'completed' : 'needsAction'; target}]
}

tabbedPane(rootPane, selectedIndex: rootPane.tabCount) {
    scrollPane(title:list.title) {
        tableFormat = defaultWritableTableFormat(
            columns: [[name: "Title"], createStatusColumn(), [name: "Updated"]],
            read: { target, columns, index -> target."${columns[index].toLowerCase()}" },
            write: { target, columns, index, value -> target."${columns[index].toLowerCase()}" = value; target},
            editable: {  target, columns, index -> true}
        )
        tasksTableModel = eventTableModel(
                source: model.tasksList, 
                format: tableFormat
            )
        tasksTable = table(id: 'links', model: tasksTableModel) 
    }
}
